package com.gym.fileserver.controller;

import com.google.common.collect.Maps;
import com.twt.common.pojo.Result;
import com.gym.fileserver.service.FileServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

@RestController
@RequestMapping("/fileserver")
public class FileServerController {
    @Autowired
    private HttpServletResponse response;

    @Autowired
    private FileServerService fileServerService;

    @PostMapping("/uploadImg")
    public Result uploadImg(MultipartFile file){
        String url = fileServerService.uploadFile(file);
        HashMap<String,String> obj = Maps.newHashMap();
        obj.put("url",url);
        return Result.ok(obj);
    }

    @GetMapping("/showImg/{imgName}")
    public void showImg(@PathVariable("imgName") String imgName){
        fileServerService.downloadFile(response,imgName);
    }
}
